package com.cskaoyan.service;

import com.cskaoyan.bean.bo.WxAddressSaveBO;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketAddress;
import com.cskaoyan.bean.po.MarketAddressExample;
import com.cskaoyan.bean.po.MarketRegion;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.cskaoyan.mapper.MarketRegionMapper;
import com.github.pagehelper.PageInfo;
import net.sf.jsqlparser.statement.create.function.CreateFunction;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

/**
 * @author wei
 * @since 2022/09/08 19:33
 */
@Service
public class WxAddressServiceImpl implements WxAddressService {
    @Autowired
    MarketAddressMapper addressMapper;
    @Autowired
    MarketRegionMapper regionMapper;

    @Override
    public CommonData queryAddressList() {

        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");
        Integer userId = user.getId();

        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();

        criteria.andUserIdEqualTo(userId);

        List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
        CommonData<MarketAddress> objectCommonData = new CommonData<>();
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>();
        int pages = marketAddressPageInfo.getPages();
        long num = marketAddressPageInfo.getTotal();
        int limit = marketAddressPageInfo.getPageSize();
        int page = marketAddressPageInfo.getPageNum();
//        int num = marketAddresses.size();
//        int limit = 2;
//        int pages = num / limit + 1;
//        int page = 1;
        objectCommonData.setList(marketAddresses);
        objectCommonData.setLimit(limit);
        objectCommonData.setPages(pages);
        objectCommonData.setPage(page);
        return objectCommonData;
    }

    @Override
    public int addAddress(WxAddressSaveBO wxAddressSaveBO) {
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");
        Integer userId = user.getId();


        Integer addressId = wxAddressSaveBO.getId();
//        String province = wxAddressSaveBO.getProvince();
//        String city = wxAddressSaveBO.getCity();
        String county = wxAddressSaveBO.getCounty();

        int code=regionMapper.selectCodeByCounty(county);

        if (addressId == 0) {

            if (wxAddressSaveBO.getIsDefault()) {
                MarketAddressExample marketAddressExample = new MarketAddressExample();
                MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
                criteria.andUserIdEqualTo(userId);
                List<MarketAddress> marketAddresses = addressMapper.selectByExample(marketAddressExample);
                for (MarketAddress marketAddress : marketAddresses) {
                    marketAddress.setIsDefault(false);
                    addressMapper.updateByPrimaryKeySelective(marketAddress);
                }
            }

            MarketAddress marketAddress = new MarketAddress();
            BeanUtils.copyProperties(wxAddressSaveBO, marketAddress);

            marketAddress.setPostalCode(String.valueOf(code));

            Date date = new Date(System.currentTimeMillis());
            marketAddress.setAddTime(date);
            marketAddress.setUpdateTime(date);
            marketAddress.setUserId(userId);
            addressMapper.insertSelective(marketAddress);
            Integer id = marketAddress.getId();
            return id;
        }
        else {
           MarketAddress address= new MarketAddress();
            BeanUtils.copyProperties(wxAddressSaveBO, address);
            Date date = new Date(System.currentTimeMillis());
            address.setUpdateTime(date);

            address.setPostalCode(String.valueOf(code));

           addressMapper.updateByPrimaryKeySelective(address);
            Integer id = address.getId();
            return id;
        }
    }


    @Override
    public void addressDelete(Integer id) {
        addressMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MarketAddress queryAddressDetail(Integer id) {
        MarketAddress marketAddress = addressMapper.selectByPrimaryKey(id);

        return marketAddress;
    }


}

